package com.zmc.admin.controller.system;

import com.zmc.admin.application.LoginApplicationService;
import com.zmc.admin.infrastructure.bean.query.SysUserIdentityQuery;
import com.zmc.admin.infrastructure.bean.vo.user.SysIdentityVo;
import com.zmc.admin.infrastructure.bean.vo.identity.UserIdentityVo;
import com.zmc.admin.infrastructure.constant.ErrorCodeConstants;
import com.zmc.admin.infrastructure.entity.SysUserEntity;
import com.zmc.admin.infrastructure.entity.SysUserIdentityEntity;
import com.zmc.admin.infrastructure.service.SysUserIdentityService;
import com.zmc.admin.infrastructure.service.SysUserService;
import com.zmc.common.annotation.PreventRepeatSubmit;
import com.zmc.common.domain.freight.FreightSendAddress;
import com.zmc.common.enums.RequestSourceEnum;
import com.zmc.common.exception.base.WebDisplayException;
import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.R;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



/**
 * 公告 信息操作处理
 * @author zmc
 */
@RestController
@RequestMapping("/identity")
public class SysUserIdentityController {

    @Autowired
    private SysUserIdentityService sysUserIdentityService;

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private LoginApplicationService loginApplicationService;

    @PostMapping("/list")
    public R list(@RequestBody SysUserIdentityQuery query) {
        PageUtils page = sysUserIdentityService.queryPage(query);
        return R.page(page);
    }

    @PostMapping(value = "/info")
    public R getInfo(@RequestBody SysIdentityVo vo) {
        return R.ok(sysUserIdentityService.getById(vo.getId()));
    }

    @PostMapping("/approve")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R approve(@RequestBody UserIdentityVo vo) {
        loginApplicationService.approve(vo);
        return R.ok();
    }

    @PostMapping("/get/{merchantId}")
    public R getUserIdentity(@PathVariable("merchantId") String merchantId) {
        SysUserEntity user = sysUserService.getMerchantId(merchantId);
        SysUserIdentityEntity identityEntity = sysUserIdentityService.getByUserId(user.getUserId());
        if (identityEntity == null) {
            throw new WebDisplayException(ErrorCodeConstants.USER_NO_IDENTITY_EXCEPTION);
        }
        FreightSendAddress freightSendAddress = new FreightSendAddress();
        freightSendAddress.setPhone(user.getPhonenumber());
        freightSendAddress.setPostCode(identityEntity.getPostCode());
        freightSendAddress.setProvince(identityEntity.getProvince());
        freightSendAddress.setCity(identityEntity.getCity());
        freightSendAddress.setRegion(identityEntity.getRegion());
        freightSendAddress.setAddress(identityEntity.getDetailAddress());
        return R.ok(freightSendAddress);
    }

}
